package com.lyf.scm.plan.domain.repository;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.plan.api.dto.DemandPlanDTO;
import com.lyf.scm.plan.domain.convertor.DemandPlanConvertor;
import com.lyf.scm.plan.domain.convertor.DemandPlanSelectConvertor;
import com.lyf.scm.plan.domain.remote.bigdata.dto.BigDataGeneralInfoDTO;
import com.lyf.scm.plan.insfrastructure.db.dataobject.DemandPlanDO;
import com.lyf.scm.plan.insfrastructure.db.dataobject.SalesPlanSelectDO;
import com.lyf.scm.plan.insfrastructure.db.dataobject.bigdata.SafetyStockUpDO;
import com.lyf.scm.plan.insfrastructure.db.mapper.DemandPlanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * description:
 * 需求计划
 *
 * @author: haodong
 * @data: 2020/8/7 16:31
 */
@Service
@Slf4j
public class DemandPlanRepository {

    @Resource
    private DemandPlanConvertor demandPlanConvertor;
    @Resource
    private DemandPlanSelectConvertor demandPlanSelectConvertor;
    @Resource
    private DemandPlanMapper demandPlanMapper;


    public PageInfo<DemandPlanDO> listWeekBySelective(DemandPlanDTO demandPlan) {
        PageHelper.startPage(demandPlan.getPageNum(), demandPlan.getPageSize());
        SalesPlanSelectDO salesPlanSelectDO = demandPlanSelectConvertor.dtoToDO(demandPlan);
        return new PageInfo(demandPlanMapper.listDemandPlanBySelective(salesPlanSelectDO));
    }

    public List<DemandPlanDTO> listSalesAndDirectTarget(DemandPlanDO demandPlanDO) {
        return demandPlanConvertor.doToDTO(demandPlanMapper.listSalesAndDirectTarget(demandPlanDO));
    }

    public List<DemandPlanDTO> listAllByCycles(DemandPlanDTO demandPlan) {
        return demandPlanConvertor.doToDTO(demandPlanMapper.listAllByCycles(demandPlanSelectConvertor.dtoToDO(demandPlan)));
    }

    public List<DemandPlanDO> listExistData(List<DemandPlanDO> demandPlanDOS) {
        return demandPlanMapper.listExistData(demandPlanDOS);
    }

    public Integer updateData(List<DemandPlanDO> updateList) {
        return demandPlanMapper.updateData(updateList);
    }

    public Integer saveData(List<DemandPlanDO> insertList) {
        return demandPlanMapper.saveData(insertList);
    }

    public List<DemandPlanDTO> listAllByCondition(DemandPlanDTO demandPlanDTO) {
        return demandPlanConvertor.doToDTO(demandPlanMapper.listAllByCondition(demandPlanSelectConvertor.dtoToDO(demandPlanDTO)));
    }

    /**
     * 大数据获取安全库存数据
     *
     * @param storeCode
     * @param startDate
     * @return {@link List<SafetyStockUpDO>}
     **/
    public List<SafetyStockUpDO> listUpByStoreAndDate(String storeCode, Date startDate) {
        return demandPlanMapper.listUpByStoreAndDate(storeCode, startDate);
    }

    public void updateSafetyStockById(List<BigDataGeneralInfoDTO> bigDataGeneralInfoDTOs,String opSign,String storeCode,Date startDate) {
        demandPlanMapper.updateSafetyStockById(bigDataGeneralInfoDTOs,opSign,storeCode,startDate);
    }

    public void delete(String opSign){
        demandPlanMapper.delete(opSign);
    }

    public void deleteByOpSign(String opSign) {
        demandPlanMapper.deleteByOpSign(opSign);
    }
}
